package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.AGoods;
import com.ruoyi.system.domain.ATCommerceOrders;
import com.ruoyi.system.service.IAGoodsService;
import com.ruoyi.system.service.IATCommerceOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.success;

@RestController
@RequestMapping("/system/index")
public class IndexController {

    @Autowired
    private IATCommerceOrdersService iatCommerceOrdersService;

    @Autowired
    private IAGoodsService iaGoodsService;

    //获取销售总额
    @GetMapping("/all")
    public AjaxResult allxiaosho(ATCommerceOrders atCommerceOrders){
        DecimalFormat df = new DecimalFormat("#.00");
        Double money = new Double(0);
        atCommerceOrders.setStatus(2L);
        List<ATCommerceOrders> atCommerceOrdersList = iatCommerceOrdersService.selectATCommerceOrdersList(atCommerceOrders);
        for (ATCommerceOrders commerceOrders : atCommerceOrdersList) {
            AGoods aGoods = iaGoodsService.selectAGoodsById(commerceOrders.getGoodsid());
            money+=(aGoods.getMoney())*(commerceOrders.getNums());
        }
        atCommerceOrders.setStatus(3L);
        atCommerceOrdersList = iatCommerceOrdersService.selectATCommerceOrdersList(atCommerceOrders);
        for (ATCommerceOrders commerceOrders : atCommerceOrdersList) {
            AGoods aGoods = iaGoodsService.selectAGoodsById(commerceOrders.getGoodsid());
            money+=(aGoods.getMoney())*(commerceOrders.getNums());
        }
        return success(df.format(money));
    }
    @GetMapping("/nums")
    public AjaxResult nums(ATCommerceOrders atCommerceOrders){
        List<ATCommerceOrders> atCommerceOrdersList  = iatCommerceOrdersService.selectATCommerceOrdersList(atCommerceOrders);
        return success("操作成功",atCommerceOrdersList.size());
    }
    //获取一段时间内的商品销售额排名，前7名
    @GetMapping("/rank")
    public AjaxResult rank(ATCommerceOrders atCommerceOrders){
        DecimalFormat df = new DecimalFormat("#.00");
        List<ATCommerceOrders> list = iatCommerceOrdersService.rank(atCommerceOrders);
        for (ATCommerceOrders commerceOrders : list) {
            commerceOrders.setaGoods(iaGoodsService.selectAGoodsById(commerceOrders.getGoodsid()));
            commerceOrders.setMoney(Double.parseDouble(df.format(commerceOrders.getaGoods().getMoney()*commerceOrders.getNums())));
        }
        list.sort((ATCommerceOrders a1,ATCommerceOrders a2)->{
            return a1.getMoney()>a2.getMoney()?-1:1;
        });
        return success("操作成功",list.subList(0,Math.min(7,list.size())));
    }

}
